<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Dcat\Admin\Admin;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\AdminUser
 *
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $name
 * @property string|null $avatar
 * @property string|null $remember_token
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\User|null $user
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser query()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUser whereUsername($value)
 * @mixin \Eloquent
 * @property-read \App\Models\User|null $userModel
 */
class AdminUser extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'admin_users';

    protected $guarded = [];

    public const ROLE_ADMIN = 1;
    public const ROLE_AGENT = 2;
    public const ROLE_EMPLOYER = 3;
    public static $lib_role = [
        self::ROLE_ADMIN => '管理员',
        self::ROLE_AGENT => '代理',
        self::ROLE_EMPLOYER => '员工',
    ];

    public function userModel()
    {
        return $this->hasOne(User::class, 'admin_id');
    }

    public static function user_id()
    {
        $user = User::query()->where('admin_id', Admin::user()->id)->firstOrFail();
        return $user->id;
    }

    /**
     * @param int $current_role
     * @return array|string[]
     */
    public static function roleList()
    {
        $roleModel = config('admin.database.roles_model');
        $optiosn_roles = $roleModel::all()->pluck('name', 'id') ?: self::$lib_role;

        if (Admin::user()->isRole('administrator') || Admin::user()->isRole('agent')) {
            unset($optiosn_roles[self::ROLE_ADMIN]);
        } elseif (Admin::user()->isRole('employer')) {
            $optiosn_roles = [];
        }

        return $optiosn_roles;
    }

    public function isAgent()
    {
        $record_role = DB::table('admin_role_users')
            ->leftJoin('admin_roles', 'admin_roles.id', 'admin_role_users.role_id')
            ->where('user_id', $this->id)
            ->first();
        return $record_role && in_array($record_role->slug, ['administrator', 'agent',]);
    }

    public function isEmployee(){
        $record_role = DB::table('admin_role_users')
            ->leftJoin('admin_roles', 'admin_roles.id', 'admin_role_users.role_id')
            ->where('user_id', $this->id)
            ->first();
        return $record_role && in_array($record_role->slug, ['employee',]);
    }

    public function adminRole()
    {
        $record_role = DB::table('admin_role_users')
            ->leftJoin('admin_roles', 'admin_roles.id', 'admin_role_users.role_id')
            ->where('user_id', $this->id)
            ->first();

        return $record_role->name;
    }

    /**
     * @param $admin_id
     * @return mixed
     */
    public static function userFull($admin_id)
    {
        return Cache::remember("admin:user:{$admin_id}", 3600, function() use($admin_id){
            $model_admin_user = self::findOrFail($admin_id);
            return $model_admin_user ? $model_admin_user->user: null;
        });
    }
}
